<template>
  <div>
  <van-search
  left='返回'
  v-model="value"
  show-action
  label="关键字"
  placeholder="请输入搜索关键词"
  @search="onSearch"
>
  <template #action>
    <div @click="onSearch">搜索</div>
  </template>
</van-search>
<div class="hiswarp">
  <p>搜索记录</p>
<div class="his">
  <div class="hisli" v-if="!hisitems.length">暂无搜索记录</div>
  <div class="hisli" @click="clickhis(his)" v-for="his in hisitems" :key="his">{{ his }}</div>
</div>
</div>
  </div>
</template>

<script>
import { Toast } from 'vant'
export default {
  data () {
    return {
      value: '',
      hisitems: localStorage.getItem('hisli')
        ? JSON.parse(localStorage.getItem('hisli'))
        : []
    }
  },
  methods: {
    clickhis (his) {
      this.value = his
      this.onSearch()
    },

    onSearch () {
      let hisli = localStorage.getItem('hisli')
        ? JSON.parse(localStorage.getItem('hisli'))
        : []
      hisli.unshift(this.value)
      hisli = [...new Set(hisli)]
      localStorage.setItem('hisli', JSON.stringify(hisli))
      if (this.value) {
        this.$router.push({
          path: '/searchItems',
          query: {
            key: this.value
          }
        })
      } else {
        Toast('请输入关键字')
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.hiswarp{
  p{
    margin: 0;
    font-size: 20px;
    color: #ccc;
  }
}
.his{
  display: flex;
  flex-wrap: wrap;
}
.hisli{
margin:0 10px 5px 0;
display: block;
padding: 5px;
height: 30px;
background-color: aqua;
text-align: center;
line-height: 30px;
border-radius: 10px;
}
</style>
